﻿using UnityEngine;
using System.Collections;

public class ShopBtn : MonoBehaviour {

    public TRUCK_EVENT EventID;
	public int FirstDept;

	public bool Activate = false;

    bool m_bIsOver;
    float m_fHoverSpeed;

    void OnEnable()
    {
        transform.localPosition = new Vector3(37.5f, transform.localPosition.y);
        m_bIsOver = false;
    }
	
	public void OnClick(){
		
		if (!Activate) {
            if (GameObject.Find("ShopPanel").transform.childCount == 0)
            {
                (Instantiate(Resources.Load("Prefaps/UI/Right/Shop/ShopWdw") as GameObject) as GameObject).transform.parent = GameObject.Find("ShopPanel").transform;
                GameObject.Find("ShopWdw(Clone)").GetComponent<ShopWdw>().EventID = this.EventID;

                gameObject.SetActive(false);
            }
            else
            {
                GameObject.Find("ShopPanel").transform.GetChild(0).gameObject.SetActive(true);
                GameObject.Find("ShopWdw(Clone)").GetComponent<ShopWdw>().Initializing();

                gameObject.SetActive(false);
            }
			    
            //transform.localPosition = new Vector3 ( 120, transform.localPosition.y);
			Activate = true;
		}
		
	}
	
	public void WdwClose(){
		Activate = false;
		transform.localPosition = new Vector3 ( 11.52f, transform.localPosition.y);
	}


	// Use this for initialization
	void Start() {
        m_fHoverSpeed = 250f * Time.fixedDeltaTime;
        transform.localScale = new Vector3(0.2f, 2);

        GetComponent<BoxCollider>().center = new Vector3(-60, -10.82f);
        GetComponent<BoxCollider>().size = new Vector3(455, -10.4f);
	}

	public void DelThis(){
		GetComponent<TweenPosition> ().Play (false);
		GetComponent<TweenPosition> ().to.x = -20.0f;
	}

    void OnHover(bool isOver)
    {
        if (isOver)
            m_bIsOver = true;
        else
            m_bIsOver = false;
    }

	void Update(){
		if(transform.localPosition.x <= -15.0f)
			Destroy(gameObject);

        if (m_bIsOver)
        {
            if (transform.localPosition.x > 19)
                transform.localPosition = new Vector3(transform.localPosition.x - m_fHoverSpeed, transform.localPosition.y);
            else
                transform.localPosition = new Vector3(19, transform.localPosition.y);
        }
        else
        {
            if (transform.localPosition.x < 37.5)
                transform.localPosition = new Vector3(transform.localPosition.x + m_fHoverSpeed, transform.localPosition.y);
            else
                transform.localPosition = new Vector3(37.5f, transform.localPosition.y);
        }

        if (Input.GetKeyUp(KeyCode.F4))
            OnClick();
	}
}
